package com.reader.cms.controller.sys;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.entity.sys.SysMessage;
import com.reader.cms.mapper.sys.SysMessageMapper;
import com.reader.cms.service.sys.ISysMessageService;
import com.reader.cms.controller.BaseController;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.exception.CustomException;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jiahaikun
 * @description 功能描述
 * @date 2018-08-06
 */
@RestController
@RequestMapping("/api/SysMessage")
public class SysMessageController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(SysMessageController.class);

    @Autowired
    public ISysMessageService iSysMessageService;

    @Autowired
    SysMessageMapper sysMessageMapper;

    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param code        信息code
     * @param message     信息名称
     * @return
     * @function 分页查询数据
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                                    @RequestParam(value = "size") int size,
                                                    @RequestParam(value = "code") String code,
                                                    @RequestParam(value = "message") String message) {
        ResultMessageInfo resultMessage = null;
        Page<Map<String, Object>> list = iSysMessageService.selectListPage(currentPage, size, code, message);
        resultMessage = ResultMessageInfo.ofSuccess(list);
        return resultMessage;
    }

    /**
     * @param sysMessage 实体对象
     * @return
     * @function 添加新记录
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody SysMessage sysMessage) {
        ValidatorUtil.validateEntity(sysMessage, AddGroup.class);//非空验证
        SysMessage sysMsg = new SysMessage();
        sysMsg.setCode(sysMessage.getCode());
        SysMessage message = sysMessageMapper.selectOne(sysMsg);
        if(message != null){
            if(message.getDelFlag() == 1){
                sysMessage.setId(message.getId());
            }else {
                throw new CustomException("ERR_10006");
            }
        }
        ResultMessageInfo resultMessage = null;
        sysMessage.setInsertDt(new Date());
        sysMessage.setUpdtDt(new Date());
        boolean resultValue = iSysMessageService.insert(sysMessage);
        if (resultValue) {
            resultMessage = ResultMessageInfo.ofSuccess( sysMessage);
        } else {
            throw new CustomException("ERROR_10000");
        }
        return resultMessage;
    }

    /**
     * @param sysMessage 实体对象
     * @return
     * @function 修改
     */
    @PostMapping("update")
    @Transactional
    @ApiOperation(value = "跟据ID列新记录", notes = "id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo update(@RequestBody SysMessage sysMessage) {
        ValidatorUtil.validateEntity(sysMessage, AddGroup.class);//非空验证
        ResultMessageInfo resultMessage = null;
        SysMessage sysMsg = new SysMessage();
        sysMsg.setCode(sysMessage.getCode());
        SysMessage message = sysMessageMapper.selectOne(sysMsg);

        sysMessage.setUpdtDt(new Date());
        boolean resultValue = iSysMessageService.updateById(sysMessage);
        if (resultValue) {
            if (sysMessage.getDelFlag() == 1) {
                resultMessage = this.getSuccessResultMessage("MSG_10014", sysMessage);
            } else {
                resultMessage = this.getSuccessResultMessage("MSG_10013", sysMessage);
            }
        } else {
            if (sysMessage.getDelFlag() == 1) {
                throw new CustomException("ERR_10001");
            } else {
                throw new CustomException("ERR_10002");
            }

        }
        return resultMessage;
    }

    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除对象
     */
    @PostMapping("deleteIds")
    @Transactional
    @ApiOperation(value = "跟据ID删除记录", notes = "键为ids值为整型数组的json格式")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "id列表", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo deleteBatchIds(@RequestBody String data) {
        ResultMessageInfo resultMessage = null;
        List<Long> ids = JSON.parseArray(JSON.parseObject(data).getString("ids"), Long.class);
        boolean resultValue = iSysMessageService.deleteBatchIds(ids);
        if (resultValue) {
            resultMessage = ResultMessageInfo.ofSuccess( "删除成功!", resultValue);
        } else {
            return ResultMessageInfo.ofMessage("删除失败!");
        }
        return resultMessage;
    }

}